package com.yankaizhang.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yankaizhang.blog.entity.Article;
import com.yankaizhang.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

@Controller
public class HomeController {

    @Autowired
    ArticleService articleService;

    @RequestMapping({"/", "/index", "/post"})
    public String index(Model model,
                        @RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "size", defaultValue = "20") int size){

        PageHelper.startPage(page, size);
        List<Article> list = articleService.list(new QueryWrapper<Article>().orderByDesc("create_time"));
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("link", "/index");
        addPageInfo(model, "最新文章", "index");
        return "site/index";
    }

    @RequestMapping("/about")
    public String about(Model model){
        addPageInfo(model, "关于","about");
        return "site/about";
    }

    @RequestMapping("/archive")
    public String archives(Model model){
        addPageInfo(model, "文章归档", "archive");
        HashMap<Integer, List<Article>> archiveMap = articleService.getArchiveMap();
        model.addAttribute("archiveMap", archiveMap);
        return "site/archive";
    }

    @RequestMapping("/tags/{tag}")
    public String tags(Model model,
                       @RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "size", defaultValue = "20") int size,
                       @PathVariable(value = "tag") String tag) throws UnsupportedEncodingException {
        String decode = URLDecoder.decode(tag, "utf-8");
        System.out.println(decode);
        List<Article> records = articleService.getArticleByTag(decode);
        PageInfo<Article> pageInfo = new PageInfo<>(records);
        model.addAttribute("tag", decode);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("link", "/tags/"+tag);
        addPageInfo(model, decode, "index");
        return "site/tags";
    }

    @RequestMapping("/post/{aid}")
    public String post(@PathVariable(name = "aid") int aid, Model model){
        Article article = articleService.getById(aid);
        model.addAttribute("article", article);
        addPageInfo(model, article.getTitle(),"post");
        updateArticleViews(article);
        return "site/post";
    }

    @RequestMapping("/toLogin")
    public String toLogin(Model model){
        return "admin/login";
    }

    private void addPageInfo(Model model, String title, String pageName){
        model.addAttribute("title", title);
        model.addAttribute("pageName", pageName);
    }

    /**
     * 更新文章的点击率
     */
    private void updateArticleViews(Article article) {
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", article.getId());
        article.increaseView(1);
        articleService.update(article, updateWrapper);
    }
}
